package com.zmn.mcc.admin.controller.sys;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.admin.controller.sys.SysBaseController;
import com.zmn.mcc.model.entity.sys.McSys;
import com.zmn.mcc.model.entity.sys.McSysQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/15 10:45
 */
@Controller
@RequestMapping("/sys/")
public class SysListController extends SysBaseController {


    /**
     * 系统列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @ModelAttribute McSysQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setSysId(Integer.parseInt(keyword));
            query.setSysName("");
        } else {
            query.setSysName(keyword);
        }

        List<McSys> items = sysService.listPageByQuery(query);

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("keyword", keyword);

        return "sys/list";
    }

    /**
     * 系统详情
     */
    @RequestMapping("detail")
    public String detail(HttpServletRequest request,
                         @RequestParam(value = "sysId", defaultValue = "0") Integer sysId,
                         Model model) {

        McSys obj = NumberUtil.isNotNullOrZero(sysId) ? sysService.findByKey(sysId) : new McSys();

        model.addAttribute("obj", obj);

        return "sys/detail";
    }
}
